﻿#include "../../../Public/Window/PTWWindow.h"
#include "../../../Public/System/PTSThread.h"

#include <jni.h>

extern "C" JNIEXPORT void JNICALL Java_org_PatriotEngine_Runtime_PTWindowImpl_OnNativeWindowCreated(JNIEnv *pEnv, jobject rThis, jobject rSurface);
extern "C" JNIEXPORT void JNICALL Java_org_PatriotEngine_Runtime_PTWindowImpl_OnNativeWindowDestroyed(JNIEnv *pEnv, jobject rThis, jobject rSurface);
extern "C" JNIEXPORT void JNICALL Java_org_PatriotEngine_Runtime_PTWindowImpl_OnNativeWindowChanged(JNIEnv *pEnv, jobject rThis, jobject rSurface, jint format, jint width, jint height);
extern "C" JNIEXPORT void JNICALL Java_org_PatriotEngine_Runtime_PTWindowImpl_OnMotionEventDispatch(JNIEnv *pEnv, jobject rThis, jint action, jfloat x, jfloat y, jint pointerIndex);
extern "C" JNIEXPORT void JNICALL Java_org_PatriotEngine_Runtime_PTWindowImpl_OnActivityCreated(JNIEnv *pEnv, jobject rThis);
extern "C" JNIEXPORT void JNICALL Java_org_PatriotEngine_Runtime_PTWindowImpl_OnActivityDestroyed(JNIEnv *pEnv, jobject rThis);

class PTWWindowImpl :public IPTWWindow
{
	//
	ANativeWindow * volatile m_hWindow_Cache;

	void * volatile m_pOutputEventCallback_UserData;
	void (PTPTR * volatile m_pOutputEventCallback)(void *pUserData, void *pOutputData);

	//Java主线程调用
	friend void JNICALL Java_org_PatriotEngine_Runtime_PTWindowImpl_OnNativeWindowCreated(JNIEnv *pEnv, jobject rThis, jobject rSurface);
	friend void JNICALL Java_org_PatriotEngine_Runtime_PTWindowImpl_OnNativeWindowDestroyed(JNIEnv *pEnv, jobject rThis, jobject rSurface);
	friend void JNICALL Java_org_PatriotEngine_Runtime_PTWindowImpl_OnNativeWindowChanged(JNIEnv *pEnv, jobject rThis, jobject rSurface, jint format, jint width, jint height);
	friend void JNICALL Java_org_PatriotEngine_Runtime_PTWindowImpl_OnActivityCreated(JNIEnv *pEnv, jobject rThis);
	friend void JNICALL Java_org_PatriotEngine_Runtime_PTWindowImpl_OnActivityDestroyed(JNIEnv *pEnv, jobject rThis);

	void * volatile m_pInputEventCallback_UserData;
	void (PTPTR * volatile m_pInputEventCallback)(void *pUserData, void *pInputData);

	friend void JNICALL Java_org_PatriotEngine_Runtime_PTWindowImpl_OnMotionEventDispatch(JNIEnv *pEnv, jobject rThis, jint action, jfloat x, jfloat y, jint pointerIndex);

	//IPTWindow
	void OutputEventCallback_Hook(void *pUserData, void(PTPTR *pOutputEventCallback)(void *pUserData, void *pOutputData))override;
	void InputEventCallback_Hook(void *pUserData, void(PTPTR *pInputEventCallback)(void *pUserData, void *pInputData)) override;
	void Parent_Set(PTWHWindow hWindowParent) override;
	void Position_Set(uint32_t TopLeftX, uint32_t TopLeftY) override;
	void Size_Set(uint32_t Width, uint32_t Height) override;


	//HookMessagePump
	int m_hSocketRead;
	int m_hSocketWrite;

	JNIEnv *m_pEnv_Cache;
	jobject m_rActivity_Cache;

	//Java主线程调用
	friend void JNICALL Java_org_PatriotEngine_Runtime_PTWindowImpl_OnActivityCreated(JNIEnv *pEnv, jobject rThis);

public:
	//Java主线程调用
	void HookMessagePump();
	static int HookMessagePump_ALooperCallback(int fd, int events, void* data);
	enum
	{
		PTCommandType_Unknown = 0,
		PTCommandType_OutputEventCallback_Hook = 1,
		PTCommandType_TermminateMessagePump = 2
	};

	//Static
	inline PTWWindowImpl();
	inline ~PTWWindowImpl();

	//PTInvokeMain线程调用
	void TermminateMessagePump();
};

